package cn.cxj.pet.controller;

import cn.cxj.basic.exception.BusinessException;
import cn.cxj.basic.util.AjaxResult;
import cn.cxj.basic.util.PageList;
import cn.cxj.pet.domain.Pet;
import cn.cxj.pet.query.PetQuery;
import cn.cxj.pet.service.IPetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/pet")
public class PetController {

    @Autowired
    private IPetService petService;

    @PostMapping
    public PageList<Pet> getAll(@RequestBody PetQuery petQuery){
        return petService.queryPage(petQuery);
    }

    /**
     * 批量上架
     * @param ids
     * @return
     */
    @PostMapping("/onsale")
    public AjaxResult onsale(@RequestBody Long[] ids){
        try {
            petService.onsale(ids);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试!");
        }
    }

    /**
     * 批量下架
     * @param ids
     * @return
     */
    @PostMapping("/offsale")
    public AjaxResult offsale(@RequestBody Long[] ids){
        try {
            petService.offsale(ids);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙,稍后重试!");
        }
    }

    /**
     * 基于id查询详情信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Pet findById(@PathVariable Long id){
        return petService.loadByIdDetail(id);
    }


}
